
// 获取应用实例
const app = getApp()
const computedBehavior = require('miniprogram-computed')
Page({
  behaviors: [ computedBehavior ],
  data: {
    countDownNumber: 60,
    getValidCodeText: '获取验证码',
    phoneNumber: '',
    validCode: ''
  },
  computed: {
    loginDisabled (data) {
      if (data.phoneNumber && data.validCode) {
        return true
      } else {
        return false
      }
    }
  },
  formValueValid () {
    const phoneRegExp = new RegExp('^1[3456789]\\d{9}$')
    const codeRegExp = new RegExp('^\\d{6}$')
    if (!(phoneRegExp.test(this.data.phoneNumber))) {
      wx.showToast({
        title: '手机号输入有误',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!(codeRegExp.test(this.data.validCode))) {
      wx.showToast({
        title: '验证码有误',
        icon: 'none',
        duration: 2000
      })
      return false
    } else {
      return true
    }
    //.test()
  },
  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (this.formValueValid()) {
      console.log(123)
      app.api.loginWithValidCode(e.detail.value).then(res => {
        console.log(res)
        if (res.resultCode === 0) {
        //  注册成功
          const requestData = {
            encrypteddata: app.globalData.phoneCode.encryptedData,
            iv: app.globalData.phoneCode.iv,
            wmptoken: app.globalData.wmptoken
          }
          app.api.weChatBinding(requestData).then(res => {
            console.log(res)
            if (res.resultCode === 0) {
              wx.navigateTo({
                url: '/pages/index/index'
              })
            }
            if (res.resultCode === 3002) {
              wx.showModal({
                title: '未找到当前手机号绑定信息',
                content: '是否前去注册',
                confirm: '注册',
                cancel: '暂不登录',
                success (res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/login/login'
                    })
                  }
                }
              })

            }
          })

        }
        // {
        //   "result": true,
        //   "resultCode": 0,
        //   "msg": "操作成功",
        //   "data": {
        //   "openId": null,
        //     "unionId": null,
        //     "memberId": 69,
        //     "token": "zv8eA7di6ueC1XpP/kSKo+WJOEJT7wee+SaCzPTsKK2pujQJTEgqVA181+MU7cWZ"
        // }
        // }
      })
    }
  },
  handlePhoneInput(e) {
    console.log(e.detail.value)
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  handleValidCodeInput (e) {
    console.log(e.detail.value)
    this.setData({
      validCode: e.detail.value
    })
  },
  handleValidCodeButtonClick() {
    const that = this
    if (this.data.countDownNumber === 60) {
    app.api.getValidCode({cellphone: this.data.phoneNumber}).then(res => {
      console.log(res)
      // 执行成功
      if (res.resultCode === 0) {
        that.countDown()
      }
    })
    }
  },
  countDown () {
    let time = 60
    const countInterval = setInterval(() => {
      time--
      if (time > 0) {
        this.setData({
          getValidCodeText: `剩余${time}s`,
          countDownNumber: time
        })
      }else{
        this.setData({
          getValidCodeText: `获取验证码`,
          countDownNumber: 60
        })
        clearInterval(countInterval)
      }
    },1000)
  }

});
